﻿using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL
{
    public class Namespace
    {
        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding m_Binding;

        private string m_URI;
        private string m_LocalPrefix;
        private string m_MappedPrefix;

        private System.Xml.Linq.XElement m_XElement;

        public Namespace(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding i_Binding, string i_LocalPrefix, string i_URI)
        {
            this.NamespaceInitialize
                (
                i_LocalPrefix,
                i_URI
                );

            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SimpleFeedBindingsXNamespace + Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.NAMESPACE_ELEMENT_NAME);

            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LOCAL_PREFIX_ATTRIBUTE, m_LocalPrefix);
            m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.URI_ATTRIBUTE, m_URI);
        }

        public Namespace(System.Xml.Linq.XElement i_NamespaceXElement)
        {
            if (i_NamespaceXElement == null)
                throw new System.ArgumentException("i_NamespaceXElement must be non-null!");

            bool InvalidElement =
                (i_NamespaceXElement.Name.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.NAMESPACE_ELEMENT_NAME) ||
                (i_NamespaceXElement.Name.NamespaceName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_NamespaceXElement!");

            m_XElement = i_NamespaceXElement;

            string URI = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.URI_ATTRIBUTE).Value;
            string LocalPrefix = m_XElement.Attribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Constants.LOCAL_PREFIX_ATTRIBUTE).Value;

            this.NamespaceInitialize
                (
                LocalPrefix,
                URI
                );
        }

        internal void NamespaceInitialize(string i_LocalPrefix, string i_URI)
        {
            if (System.String.IsNullOrEmpty(i_URI))
                throw new System.ArgumentException("Must provide non-null values for id and uri!");

            if (System.String.IsNullOrEmpty(i_LocalPrefix))
                throw new System.ArgumentException("Must provide non-null values for id and uri!");

            m_URI = i_URI;
            m_LocalPrefix = i_LocalPrefix;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrarySL.Binding Binding
        {
            get
            {
                return m_Binding;
            }

            internal set
            {
                if (value == null)
                    throw new System.ArgumentException("Value must be non-null!");

                m_Binding = value;
            }
        }

        public string URI
        {
            get
            {
                return m_URI;
            }
        }

        public string LocalPrefix
        {
            get
            {
                return m_LocalPrefix;
            }
        }

        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        internal void SetMappedPrefix(string i_MappedPrefix)
        {
            if (System.String.IsNullOrEmpty(i_MappedPrefix))
                throw new System.ArgumentException("Invalid value for i_MappedPrefix!");

            m_MappedPrefix = i_MappedPrefix;
        }

        public override string ToString()
        {
            return "Prefix=" + m_LocalPrefix + ", URI=" + m_URI; 
        }
    }
}
